{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.9e26d0a37f9878d783e5e3e56af674cf.315360000.1926575120.282335-23571288', 'expires_in': 2592000, 'session_key': '9mzdA5nZ866AFPQ4i14p1/SW2yoCoZFfFUurKLO3ulkilePQUiR8JD+JC7sT+G2IGxOBb3/iXB61OMVRrLeCIJC/Va9CTw==', 'access_token': '24.83beea12a9e08616a18ef13c212eb836.2592000.1613807120.282335-23571288', 'scope': 'public brain_all_scope brain_body_analysis brain_body_attr brain_body_number brain_driver_behavior brain_body_seg brain_gesture_detect brain_body_tracking brain_hand_analysis wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin', 'session_secret': 'c3efb655ac55b2bee01acd6b6fe6cdd5'}\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>refresh_token</th>\n",
       "      <td>25.9e26d0a37f9878d783e5e3e56af674cf.315360000....</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>expires_in</th>\n",
       "      <td>2592000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>session_key</th>\n",
       "      <td>9mzdA5nZ866AFPQ4i14p1/SW2yoCoZFfFUurKLO3ulkile...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>access_token</th>\n",
       "      <td>24.83beea12a9e08616a18ef13c212eb836.2592000.16...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>scope</th>\n",
       "      <td>public brain_all_scope brain_body_analysis bra...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>session_secret</th>\n",
       "      <td>c3efb655ac55b2bee01acd6b6fe6cdd5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                                                0\n",
       "refresh_token   25.9e26d0a37f9878d783e5e3e56af674cf.315360000....\n",
       "expires_in                                                2592000\n",
       "session_key     9mzdA5nZ866AFPQ4i14p1/SW2yoCoZFfFUurKLO3ulkile...\n",
       "access_token    24.83beea12a9e08616a18ef13c212eb836.2592000.16...\n",
       "scope           public brain_all_scope brain_body_analysis bra...\n",
       "session_secret                   c3efb655ac55b2bee01acd6b6fe6cdd5"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "import pandas as pd\n",
    "import json\n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=vVQXbciDY1vx3U55wYL0EwGo&client_secret=C5qkLdAm1Z0iysKjQ4cDNAC0LMaX3daX'\n",
    "response = requests.get(host)\n",
    "if response:\n",
    "    print(response.json())\n",
    "pd.json_normalize(response.json()).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.83beea12a9e08616a18ef13c212eb836.2592000.1613807120.282335-23571288'"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token = response.json()[\"access_token\"]\n",
    "access_token"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 驾驶员行为分析"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'log_id': 1352160349255630848, 'person_num': 1, 'person_info': [{'location': {'score': 0.92172157764435, 'top': 409, 'left': 476, 'width': 815, 'height': 607}, 'attributes': {'both_hands_leaving_wheel': {'score': 0.94535154104233, 'threshold': 0.75}, 'eyes_closed': {'score': 0.21840912103653, 'threshold': 0.55}, 'no_face_mask': {'score': 0.87473291158676, 'threshold': 0.75}, 'not_buckling_up': {'score': 0.43338057398796, 'threshold': 0.44}, 'smoke': {'score': 0.012930653057992, 'threshold': 0.48}, 'not_facing_front': {'score': 0.77021491527557, 'threshold': 0.5}, 'cellphone': {'score': 0.16600452363491, 'threshold': 0.69}, 'yawning': {'score': 0.0070231277495623, 'threshold': 0.5}, 'head_lowered': {'score': 0.37843972444534, 'threshold': 0.55}}}], 'driver_num': 1}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "驾驶行为分析\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/driver_behavior\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('C:/Users/86183/Pictures/api/a20.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '[24.cf98ca23ac66f76f601ff1e7acee55f2.2592000.1613805933.282335-23571288]'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "165px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
